export enum MoveDir {
    Left,
    Right,
}

const { ccclass, property } = cc._decorator;
/** 移动组件 */
@ccclass
export default class MoveComp extends cc.Component {
    /** 开始移动方向 */
    @property({
        type: cc.Enum(MoveDir),
        displayName: "开始移动方向"
    })
    public StartDir: MoveDir = MoveDir.Left;
    /** 移动速度 */
    @property({
        type: cc.Float,
        displayName: "移动速度"
    })
    public MoveSpeed: number = 100;
    /** 宽度 */
    @property({
        type: cc.Float,
        displayName: "宽度"
    })
    public Width: number = 66;

    /** 当前正负号 */
    private curDeltaSign = 1;

    start() {
        this.curDeltaSign = this.StartDir === MoveDir.Left ? -1 : 1;
    }

    update(dt) {
        this.node.x += dt * this.MoveSpeed * this.curDeltaSign;
        if (this.node.x <= -cc.winSize.width / 2 + this.Width ||
            this.node.x >= cc.winSize.width / 2 - this.Width) {
            this.curDeltaSign = -this.curDeltaSign;
            this.node.scaleX = -this.node.scaleX;
            this.node.x += dt * this.MoveSpeed * this.curDeltaSign;
        }
    }
}
